package com.stars.empoly.utils

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.layout.QMUIFrameLayout
import com.qmuiteam.qmui.layout.QMUILinearLayout
import com.qmuiteam.qmui.skin.QMUISkinValueBuilder
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.popup.QMUIPopup
import com.qmuiteam.qmui.widget.popup.QMUIPopups
import com.stars.empoly.R
import com.stars.empoly.adapter.ChoseDateAdapter
import com.stars.empoly.adapter.PointsTabsAdapter
import com.stars.empoly.inte.ChoiceData
import com.stars.empoly.model.DateBean
import com.stars.empoly.model.bean.CateTypeBean
import com.trailer.base.utils.UiUtils
import com.trailer.base.utils.UiUtils.*

/**
 * 去选择时间
 *Created by shihuiyun
 *on 2020/9/21
 */
object  PopuChoseDateUtils {

    fun show(context: Context, list:List<DateBean>,view: View, choiceData:ChoiceData){

        var mNormalPopup = QMUIPopups.fullScreenPopup(context)
        val builder: QMUISkinValueBuilder = QMUISkinValueBuilder.acquire()
        val frameLayout = QMUIFrameLayout(context)
        //自定义布局
        val layoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var layout = layoutInflater.inflate(R.layout.popu_date, null)

        val ll:LinearLayout=layout.findViewById(R.id.ll)
        val tvClose:TextView=layout.findViewById(R.id.tv_close)
        val gridview:GridView=layout.findViewById(R.id.gridview)
        val recyclerType: RecyclerView =layout.findViewById(R.id.recycler_type)
        val llSubmit: QMUILinearLayout =layout.findViewById(R.id.ll_submit)

        var choseTime:String?=""
        //
        var adapter= ChoseDateAdapter()
        recyclerType.adapter=adapter
        adapter.addData(list[0].timeList)


        //设置搜索标题
        var list1=ArrayList<CateTypeBean>()
        for ((index, item) in list.withIndex()) {
            if(index==0){
                list1.add(CateTypeBean(item.title,true))

            }else{
                list1.add(CateTypeBean(item.title,false))

            }
        }

        // item宽度
        val itemWidth = dp2Px(context, 70f)
        // item之间的间隔
        val itemPaddingH = dp2Px(context, 2f)
        val size1: Int =list1.size
        // 计算GridView宽度
        var gridviewWidth = size1 * (itemWidth + itemPaddingH)
        val params = LinearLayout.LayoutParams(
            gridviewWidth, LinearLayout.LayoutParams.MATCH_PARENT)
        gridview.layoutParams = params
        gridview.columnWidth = itemWidth
        gridview.horizontalSpacing = itemPaddingH
        gridview.stretchMode = GridView.NO_STRETCH
        gridview.numColumns = size1

       var  tabadapter = PointsTabsAdapter(context)
        tabadapter?.setDeviceList(list1,object :ChoiceData{
            override fun onRestuse(index: String?) {
                var i=index?.toInt()
                var listg=tabadapter.getData()
                for((index,item)in listg.withIndex()){
                    item.chose = index==i
                    if(index==i){
                        //更新列表
                        adapter.clearList()
                        adapter.addData(list[index].timeList)
                    }
                }
                tabadapter.notifyDataSetChanged()

            }
        })
        gridview.adapter = tabadapter

        //设置默认 时间

        adapter?.setOnItemClickListener { adapter, view, position ->

            var ss=adapter.getItem(position) as DateBean.TimeList
            if(ss.status==0){
                Toast.makeText(context,"该时间已被预约！",Toast.LENGTH_SHORT).show()

                return@setOnItemClickListener
            }

            var list=adapter.data as List<DateBean.TimeList>
            for((index,item)in list.withIndex()){
                if(index==position){
                    //选择的时间
                    choseTime=item.time
                    item.chose=true
                    adapter?.notifyItemChanged(index)
                }else{
                    if(item.chose){
                        item.chose=false
                        adapter?.notifyItemChanged(index)
                    }
                }
            }

        }





        tvClose.onClick {
            mNormalPopup.dismiss()
        }
        ll.onClick {

        }
        llSubmit.onClick {
            if(choseTime.isNullOrEmpty()){
                Toast.makeText(context,"请选择时间",Toast.LENGTH_SHORT).show()
                return@onClick
            }
            if(null!=choiceData){
                choiceData.onRestuse(choseTime)
            }
            mNormalPopup.dismiss()

        }

        builder.release()

        val size = QMUIDisplayHelper.getScreenWidth(context)
        val height = QMUIDisplayHelper.getScreenHeight(context)


        val lp: FrameLayout.LayoutParams = FrameLayout.LayoutParams(size,
                height)
        frameLayout.addView(layout, lp)


        mNormalPopup.addView(frameLayout)
        mNormalPopup.onDismiss() {
        }

        mNormalPopup.dismissIfOutsideTouch(false)
        mNormalPopup.animStyle(QMUIPopup.ANIM_GROW_FROM_CENTER)
        mNormalPopup.show(view)


    }




}
